const logService = require('../services/logService');

class LogController {
  /**
   * 创建日志
   * @param {Object} ctx - Koa context
   * @param {Function} next - Koa next middleware function
   */
  async createLog(ctx, next) {
    try {
      const { message, category } = ctx.request.body;

      const log = await logService.createLog({ message, category });

      ctx.status = 201;
      ctx.body = { log };
    } catch (error) {
      ctx.status = 500;
      ctx.body = { message: '创建日志失败', error: error.message };
    }

    await next();
  }

  /**
   * 获取所有日志
   * @param {Object} ctx - Koa context
   * @param {Function} next - Koa next middleware function
   */
  async getAllLogs(ctx, next) {
    try {
      const logs = await logService.getAllLogs();

      ctx.status = 200;
      ctx.body = { logs };
    } catch (error) {
      ctx.status = 500;
      ctx.body = { message: '获取日志列表失败', error: error.message };
    }

    await next();
  }

  /**
   * 根据日志ID获取单个日志
   * @param {Object} ctx - Koa context
   * @param {Function} next - Koa next middleware function
   */
  async getLogById(ctx, next) {
    try {
      const { id } = ctx.params;

      const log = await logService.getLogById(id);

      if (!log) {
        ctx.status = 404;
        ctx.body = { message: '找不到指定ID的日志' };
      } else {
        ctx.status = 200;
        ctx.body = { log };
      }
    } catch (error) {
      ctx.status = 500;
      ctx.body = { message: '获取日志详情失败', error: error.message };
    }

    await next();
  }

  /**
   * 更新日志
   * @param {Object} ctx - Koa context
   * @param {Function} next - Koa next middleware function
   */
  async updateLog(ctx, next) {
    try {
      const { id } = ctx.params;
      const { message, category } = ctx.request.body;

      const success = await logService.updateLog({ logId: id, message, category });

      if (!success) {
        ctx.status = 404;
        ctx.body = { message: '找不到指定ID的日志' };
      } else {
        ctx.status = 200;
        ctx.body = { success: true };
      }
    } catch (error) {
      ctx.status = 500;
      ctx.body = { message: '更新日志失败', error: error.message };
    }

    await next();
  }

  /**
   * 删除日志
   * @param {Object} ctx - Koa context
   * @param {Function} next - Koa next middleware function
   */
  async deleteLog(ctx, next) {
    try {
      const { id } = ctx.params;

      const success = await logService.deleteLog(id);

      if (!success) {
        ctx.status = 404;
        ctx.body = { message: '找不到指定ID的日志' };
      } else {
        ctx.status = 200;
        ctx.body = { success: true }
      }
    }
    catch (error) {
        ctx.status = 500;
        ctx.body = { message: '删除日志失败', error: error.message };
    }

    await next();
}

/**
 * 批量删除日志
 * @param {Object} ctx - Koa context
 * @param {Function} next - Koa next middleware function
 */
async deleteLogs(ctx, next) {
    try {
        const { logIds } = ctx.request.body;

        const success = await logService.deleteLogs({ logIds });

        if (!success) {
            ctx.status = 404;
            ctx.body = { message: '找不到指定ID的日志' };
        } else {
            ctx.status = 200;
            ctx.body = { success: true };
        }
    } catch (error) {
        ctx.status = 500;
        ctx.body = { message: '批量删除日志失败', error: error.message };
    }

    await next();
  }
}

module.exports = new LogController();
